<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="30%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-row>
          <el-col :span="10">
            <el-form-item label="分类名称" prop="classname">
              <el-input v-model="dataForm.classname"></el-input>
            </el-form-item>
          </el-col>
      </el-row>
    <!-- <el-form-item label="" prop="addtime">
      <el-input v-model="dataForm.addtime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="deletestatus">
      <el-input v-model="dataForm.deletestatus" placeholder=""></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="分类名称" prop="classname">
      <el-input v-model="dataForm.classname" placeholder="分类名称"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="是否显示" prop="display">
      <el-input v-model="dataForm.display" placeholder="是否显示"></el-input>
    </el-form-item>
    <el-form-item label="分类等级" prop="level">
      <el-input v-model="dataForm.level" placeholder="分类等级"></el-input>
    </el-form-item>
    <el-form-item label="是否推荐" prop="recommend">
      <el-input v-model="dataForm.recommend" placeholder="是否推荐"></el-input>
    </el-form-item>
    <el-form-item label="显示顺序" prop="sequence">
      <el-input v-model="dataForm.sequence" placeholder="显示顺序"></el-input>
    </el-form-item>
    <el-form-item label="上级id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="上级id"></el-input>
    </el-form-item>
    <el-form-item label="系统图标名称" prop="iconSys">
      <el-input v-model="dataForm.iconSys" placeholder="系统图标名称"></el-input>
    </el-form-item>
    <el-form-item label="分类展示类型 0:联盟店分类 1:首页分类展示" prop="iconType">
      <el-input v-model="dataForm.iconType" placeholder="分类展示类型 0:联盟店分类 1:首页分类展示"></el-input>
    </el-form-item>
    <el-form-item label="自定义图标文件id 关联附件表" prop="iconAccId">
      <el-input v-model="dataForm.iconAccId" placeholder="自定义图标文件id 关联附件表"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          addtime: '',
          deletestatus: '',
          classname: '',
          display: '',
          level: '',
          recommend: '',
          sequence: '',
          parentId: '',
          iconSys: '',
          iconType: '',
          iconAccId: ''
        },
        dataRule: {
          classname: [
            { required: true, message: '分类名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/class/getClassNameById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.classname = data.data
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/class/${!this.dataForm.id ? 'addClass' : 'modifyByIds'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'classname': this.dataForm.classname
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
